<?php
/**
 * 《银行帐号》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../finance/Bank.class.php');

class EmpBank extends Bank {
	
	public $orderby = array(
		'-' => '请选择',
		'info_id' => '员工',
		'bank' => '开户行',
		'name' => '户名',
		'code' => '帐号',	
		'type' => '类型',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('tec_option', $this->tec);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/bank/find');
		return true;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'12', '2'=>'13', '3'=>'14');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
	
			return sprintf(" visible = 1 AND info = 1 AND (office in (%s) OR oper = '%s')", $office, $emp);
		}
	
		return sprintf(" visible = 1 AND info = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	function _where($request) {
		$info_id_s = $this->_input($request['info_id_s']);
		$tec_s = $request['tec_s'];
		
		$bank_s = $request['bank_s'];
		$name_s = $request['name_s'];
		
		$type_s = $request['type_s'];
		
		$my = $this->_my();
		if (empty($my)) {
			$where = " WHERE info = 1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($info_id_s)) {
			$where .= " AND a.info_id = '" . $info_id_s . "'";
		}
		
		if (!empty($tec_s) && $tec_s <> '-') {
			$where .= " AND a.tec = '" . $tec_s . "'";
		}
		
		if (!empty($bank_s)) {
			$where .= " AND a.bank LIKE '" . $bank_s . "%'";
		}
		
		if (!empty($name_s)) {
			$where .= " AND a.name LIKE '%" . $name_s . "%'";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND a.type = " . $type_s;
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['info_t'] = $this->info[$row['info']];
				$row['info_id_t'] = Name::emp($row['info_id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/bank/list');
		return true;
	}
	
	function add($controller, $request) {
		$this->pretreat($controller);
	
		$controller->display($request, 'emp/bank/add');
		return true;
	}
	
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$bank = $request['bank'];
		$branch = $request['branch'];
		
		$name = $request['name'];
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
			
		$code = $request['code'];
		$code = str_replace(" ", "", $code);
		
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, info, info_id, bank, branch, name, zjm, code, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '1', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $emp, $bank, $branch, $name, $zjm, $code, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/bank/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$info_id = $this->_input($request['info_id']);
		$emp = $info_id;
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$tec = $request['tec'];
		
		$bank = $request['bank'];
		$branch = $request['branch'];
		$bankcode = $this->_input($request['bankcode']);
		
		$province = $request['province'];
		$city = $request['city'];
		
		$type = $request['type'];
		$zone = $request['zone'];
		
		$name = $request['name'];
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		
		$code = $request['code'];
		$code = str_replace(" ", "", $code);
		
		$content = addslashes($request['content']);
		$status = $request['status'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', info_id = '%s', tec = '%s', bank = '%s', branch = '%s', bankcode = '%s', province = '%s', city = '%s', type = '%s', zone = '%s'";
		$fmt .= ", name = '%s', zjm = '%s', code = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $info_id, $tec, $bank, $branch, $bankcode, $province, $city, $type, $zone, $name, $zjm, $code, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/bank/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		// debug($sql);
		$query = $db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _exist($emp) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE info = 1 AND info_id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp);
		// echo $sql . "\n";
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}
		
		return false;
	}
	
	function t($controller, $request) {
		$db = newdb($this->dsn_w);

		$file = "/home/sunxy/n/bank.txt";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = trim(fgets($fd));
			// echo $buf;

			if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
				continue;
			}
			
			$a = preg_split("/;/", $buf);

			// print_r($a);

			$name = trim($a[0]);
			$province = trim($a[1]);
			$city = trim($a[2]);
			$branch = trim($a[3]);
			$zone = trim($a[4]);
			$branch_name = trim($a[5]);
			$code = trim($a[6]);
			$type = trim($a[7]);
			$content = trim($a[8]);

			$fmt = "INSERT INTO %s(name, province, city, branch, zone, branch_name, code, type, content, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $name, $province, $city, $branch, $zone, $branch_name, $code, $type, $content);
			echo $sql . "\n";
			$db->query($sql);
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function _my_info($controller, $info, $id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '%s' AND info_id = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $info, $id);
		$result = $db->query($sql);

		$controller->assign_by_ref('my', $db->fetch_all($result));
	}

	function ajax_zjm($controller, $request) {

		$data = $this->_ajax_zjm($request);
		echo json_encode($data);
	}

	function _ajax_zjm($request) {
		$zjm = $request['q'];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = 1 AND zjm LIKE '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $zjm . "%");
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {

			$ret[] = $row;
		}

		return $ret;
	}
	
	function z($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE info = '1' AND status = '1'";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			// print_r($row);
			if ($this->aa($row)) {
				continue;
			}
			
			$fmt = "UPDATE %s SET status = '3', modifytime = NOW() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
		
		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function aa($row) {
		$emp = $this->id(Constant::tbl_emp, $row['info_id']);
		// print_r($emp);
		
		if (!$emp) {
			return true;
		}
		
		if ($emp['status'] <> '3') {
			return true;
		}
		
		if ($emp['state'] <> '8') {
			return true;
		}
		
		if (empty($emp['leave'])) {
			return true;
		}
		
		if (strtotime($emp['leave']) < strtotime('-2 month')) {
			return false;
		}
		
		return true;
	}
	
	function _update($emp) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = 1, modifytime = now() WHERE info = 1 AND info_id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp);
		$db->query($sql);
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = 1 AND info_id = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
	
			$ret[] = $row;
		}
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'emp/bank/_list');
		return true;
	}
}
?>